package com.hxzy.controller.resourcestore;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.util.BeanValidatorUtil;
import com.hxzy.util.ThreadLocalUtil;
import com.hxzy.common.BaseServlet;
import com.hxzy.common.ResultAjax;
import com.hxzy.entity.ResourceStore;
import com.hxzy.service.ResourceStoreService;
import com.hxzy.service.impl.ResourceStoreSeviceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @Author: lqq
 * @Description:
 * @Date: 2022/3/8 19:37
 */
@WebServlet(urlPatterns = "/RS/update")
public class UpdateServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        ThreadLocalUtil.threadLocal.get();
        try {
            ResourceStore resourceStore=super.CollectiongParameterVlues(req,ResourceStore.class);
            Set<String> validator = BeanValidatorUtil.validator(ResourceStore.class, resourceStore);
            if (validator.isEmpty()){
                ResourceStoreService service=new ResourceStoreSeviceImpl();
                int update = service.update(resourceStore);
                ResultAjax resultAjax=null;
                if (update>0){
                    resultAjax=ResultAjax.success("更新成功");
                }else {
                    resultAjax=ResultAjax.error("更新失败");
                }
                super.render(resp, JSONObject.toJSONString(resultAjax));
            }else {
                String next = validator.iterator().next();
                ResultAjax error = ResultAjax.error(next);
                super.render(resp, JSONObject.toJSONString(error));
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
